class TagEventsController < ApplicationController
  
    layout 'default'


	# adds an interest of the user from a given tag name
	def addRelation
		if params[:commit] then
			@resource = Resource.find(params[:id])
			@tags = (params[:theTag][:tagName]).split
			@tags.each {|tagName|   
			@tag = Tag.find(:first , :conditions => "name = '#{tagName}'")
			if(@tag.nil?) 
				@tag = Tag.createTagWithName(tagName)
	   		end
	   		@tag.counter = @tag.counter + 1
	   		@tag.save
	   		@user = User.find(session[:user_id])
			@tag_event = TagEvent.find(:first , :conditions => "tag_id = #{@tag.id} AND resource_id = #{@resource.id}")
			if(@tag_event.nil?) then
				TagEvent.createTagEventFromTagUserResource(@tag.id, @user.id, @resource.id)
			else 
				flash[:notice] = "tag already present"
			end			
			}
		redirect_to :controller => "resources" , :action => "show" , :id => @resource.id
		end
	end
	
	  def addTagToProfile2
		@controller2 = "tag_events"
		@object = Resource.find(params[:id])
# 		flash[:notice] = params[:id].to_s
		render :partial => "/tags/editspecial"
	  end  		
	  
  
  def destroy
    @tag_event = TagEvent.find(params[:id])
    @resource = Resource.find(@tag_event.resource_id)
    @tag_event.destroy
	redirect_to :action => "edit", :controller => "resources" , :id => @resource.id
  end	

  
  def show  
    @tag = Tag.find(params[:tag_id])
    @tag_event = TagEvent.find(:first , :conditions => "tag_id = #{params[:tag_id]} AND resource_id = #{params[:res_id]}")
	respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @tag }
    end
  end  
end
